<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 86188
  Date: 2019/1/15
  Time: 15:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="/static/js/jquery-3.3.1.js"></script>
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
    <script src="/static/layuiadmin/layui/layui.js" type="text/javascript"></script>
    <style>
        body {
            margin: 10px;
        }
    </style>
</head>
<body>
<div style="position: fixed;left: 200px">
    <input id="mnumber" name="mnumber" type="text" class="layui-input" placeholder="请输入机构编号" value="">
</div>
<div style="position: fixed;left: 384px">
    <input id="mtype" name="mtype" type="text" class="layui-input" placeholder="请选择机构类型" value="">
</div>
<div style="position: fixed;left: 573px">
    <input id="mregion" name="mregion" type="text" class="layui-input" placeholder="请输入负责地区" value="">
</div>
<div class="layui-inline">
    <input id="mname" name="mname" type="text" class="layui-input" placeholder="请输入机构名称" value="">
</div>
<div style="position: fixed;left: 800px;top: 11px;">
    <button id="sea" class="layui-btn layui-btn-normal" data-type="reload" onclick="sear()">查询</button>
</div>

<table class="layui-hide" id="demo" lay-filter="test"></table>
<script type="text/html" id="barDemo">
    <%--<a class="layui-btn layui-btn-xs" lay-event="edit">查看</a>--%>
    <a class="layui-btn layui-btn-xs" lay-event="damage">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" id="del">删除</a>
</script>
<script>
    layui.use('table', function () {
        var params = {
            mname: $('#mname').val(),
            mnumber: $('#mnumber').val(),
            mtype: $('#mtype').val(),
            mregion: $('#mregion').val()
        };
        var table = layui.table;
        //执行一个 table 实例
        table.render({
            elem: '#demo',
            url: 'getMechanismList?mname=' + encodeURIComponent(JSON.stringify(params)),   //数据接口,
            title: '',
            page: true,
            limit: 10,
            limits: [10, 20, 30, 40, 50],
            toolbar: 'default', //开启工具栏，此处显示默认图标，可以自定义模板，详见文档,
            cols: [[ //表头
                {type: 'checkbox', fixed: 'left', align: 'center'}
                , {field: 'mtime', title: '机构时间', sort: true, align: 'center'}
                , {field: 'mnumber', title: '机构编号', align: 'center', width: 220}
                , {field: 'mtype', title: '机构类型', sort: true, align: 'center'}
                , {field: 'mname', title: '机构名称', align: 'center'}
                , {field: 'mregion', title: '负责地区', align: 'center', width: 220}
                , {field: 'madd', title: '添加人', align: 'center',}
                , {fixed: 'right', title: '操作', width: 230, align: 'center', toolbar: '#barDemo'}
            ]]
            , id: "reload"
        });
    });

    //查询方法
    function sear() {
        var params = {
            mname: $('#mname').val(),
            mnumber: $('#mnumber').val(),
            mtype: $('#mtype').val(),
            mregion: $('#mregion').val()
        };
        layui.use(['laypage', 'layer', 'table'], function () {
            var laydate = layui.laydate //日期
                , laypage = layui.laypage //分页
                , table = layui.table; //表格
            table.reload('reload', {
                url: 'getMechanismList?mname=' + encodeURIComponent(JSON.stringify(params)) //数据接口
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
    }


    layui.use(['form', 'laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'], function () {
        var laydate = layui.laydate //日期
            , laypage = layui.laypage //分页
            , layer = layui.layer //弹层
            , table = layui.table //表格
            , carousel = layui.carousel //轮播
            , upload = layui.upload //上传
            , element = layui.element //元素操作
            , slider = layui.slider //滑块

        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id),
                data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 2,
                        title: '添加机构信息',
                        maxmin: true,
                        shadeColse: true,//点击遮罩关闭层
                        area: ['820px', '430px'],
                        content: 'add',
                        end: function (data) {
                            table.reload('reload', {
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                            });
                        }
                    });
                    break;
                case 'update':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        layer.open({
                            type: 2,
                            title: '修改汽车使用信息',
                            maxmin: true,
                            shadeColse: true,//点击遮罩关闭层
                            area: ['810px', '590px'],
                            content: '../car/getUpdateCar?cid=' + checkStatus.data[0].cid,
                            end: function () {
                                //关闭窗口
                                table.reload('reload', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                });
                            }
                        });
                    }
                    break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        layer.confirm("是否删除所选的信息?", function (index) {
                            let cids = [];
                            for (let i = 0; i < data.length; i++) {
                                cids[i] = data[i].cid;
                            }
                            $.ajax({
                                url: "../car/delCars",
                                type: "post",
                                data: {
                                    cids: JSON.stringify(cids)
                                },
                                dataType: "json",
                                success:
                                    function (data) {
                                        layer.msg(data.msg);
                                        table.reload('reload', {
                                            page: {
                                                curr: 1 //重新从第 1 页开始
                                            }
                                        });
                                    }
                            })
                            ;
                        });

                    }
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') {
                layer.confirm('确认删除嘛？', function (index) {
                    $.ajax({
                        url: "deleteMechanism",
                        type: "post",
                        data: {
                            mid: obj.data.mid
                        },
                        datatype: "json",
                        success: function (data) {
                            layer.msg(data.msg);
                            table.reload('reload', {
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                            });
                        }
                    });
                });
            } else if (layEvent === 'damage') {
                layer.open({
                    type: 2,
                    title: '修改机构信息',
                    maxmin: true,
                    shadeColse: true,//点击遮罩关闭层
                    area: ['1000px', '540px'],
                    content: 'selectMechanismMid?mid=' + obj.data.mid,
                    end: function () {
                        //关闭窗口
                        table.reload('reload', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                    }
                });

            }
        });
    });
</script>
</body>
</html>
